﻿namespace WXWorkSvc.Common.TagHelpers;
[HtmlTargetElement("wx-input", TagStructure = TagStructure.WithoutEndTag)]
public class WxInputTagHelper : TagHelper
{
    private readonly IHtmlGenerator _HtmlGenerator;
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;

    public WxInputTagHelper(IHtmlGenerator generator)
    {
        _HtmlGenerator = generator;
    }
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> Fors { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-readonly-")]
    public Dictionary<string, string> ReadOnlys { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-select-")]
    public Dictionary<string, IEnumerable<SelectListItem>> SelectItems { get; set; } = new();
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {

        //inputClass = "weui-input";
        //selectClass = "weui-select";
        //selectFormat = """
        //<div class="weui-cell weui-cell_select weui-cell_select-after">
        //    <div class="weui-cell__hd"><label class="weui-label">{0}</label></div>
        //    <div class="weui-cell__bd" name="{2}">
        //        {1}
        //    </div>
        //</div>
        //""";
        //stringFormat = """"
        //<div class="weui-cell">
        //    <div class="weui-cell__hd"><label class="weui-label">{0}<i class="text-danger field-validation-valid" data-valmsg-for="{2}" data-valmsg-replace="true"></i></label></div>
        //    <div class="weui-cell__bd">
        //        {1}
        //    </div>
        //</div>
        //"""";
        //requiredMsg = " ";
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "weui-cells weui-cells_form");
        output.Attributes.Add("name", "自动创建");

        var tagValidationSummary = _HtmlGenerator.GenerateValidationSummary(ViewContext);
        if (tagValidationSummary != null)
            output.Content.AppendHtml(tagValidationSummary);
        foreach (var f in Hiddens.Values)
        {
            output.Content.AppendHtml(_HtmlGenerator.GenerateHidden(ViewContext, f.ModelExplorer, f.Name, f.Model, false, null));
        }

        foreach (var kv in Fors)
        {
            var f = kv.Value;
            var tagLabel = f.GenerateLabel("weui-label");

            if (SelectItems.Remove(kv.Key, out var selectList))
            {
                var tagSelect = _HtmlGenerator.GenerateSelect(ViewContext, f.ModelExplorer, null, f.Name, selectList, false, new { @class = "weui-select" });
                output.Content.AppendHtml($"""
                                            <div class="weui-cell weui-cell_select weui-cell_select-after">
                                                <div class="weui-cell__hd">
                                                    {tagLabel.GetHtmlString()}
                                                </div>
                                                <div class="weui-cell__bd">
                                                    {tagSelect.GetHtmlString()}
                                                </div>
                                            </div>

                                            """);
            }
            else if (f.Metadata.ModelType.Name == "Boolean")
            {
                var tagInput = _HtmlGenerator.GenerateCheckBox(ViewContext, f.ModelExplorer, f.Name, (bool)f.Model, new { @class = "weui-switch" });

                output.Content.AppendHtml($"""
                                            <div class="weui-cell weui-cell_switch">
                                                <div class="weui-cell__bd">{tagLabel.GetHtmlString()}</div>
                                                <div class="weui-cell__ft">
                                                    {tagInput.GetHtmlString()}
                                                </div>
                                            </div>
                                            """);
            }
            else
            {
                f.GetHtmlAttributes(out var htmlAttributes, out var format);
                
                var tagInput = _HtmlGenerator.GenerateTextBox(ViewContext, f.ModelExplorer, f.Name, f.Model, format, htmlAttributes);
                if (ReadOnlys.Remove(kv.Key, out _))
                {
                    tagInput.Attributes.Add("readonly", "readonly");
                    tagInput.Attributes.Add("class", "weui-input readonly");
                    tagInput.MergeAttribute("type", "text", true);
                    tagInput.Attributes.Remove("data-val");
                    tagInput.Attributes.Remove("data-val-required");
                }
                else
                    tagInput.Attributes.Add("class", "weui-input");
                output.Content.AppendHtml($"""
                                            <div class="weui-cell">
                                                <div class="weui-cell__hd">
                                                    {tagLabel.GetHtmlString()}
                                                </div>
                                                <div class="weui-cell__bd">
                                                    {tagInput.GetHtmlString()}
                                                </div>
                                            </div>
                                            """);
            }
        }

    }
}

